package com.moyu.article.service.impl;

import com.github.pagehelper.PageHelper;
import com.moyu.article.domain.report.Report;
import com.moyu.article.domain.report.dto.ReportAddDTO;
import com.moyu.article.domain.report.dto.ReportQueryDTO;
import com.moyu.article.domain.report.vo.ReportVO;
import com.moyu.article.mapper.PostMapper;
import com.moyu.article.mapper.ReportMapper;
import com.moyu.article.service.IReportService;
import com.moyu.common.core.domain.LoginUser;
import com.moyu.common.core.enums.ResultCode;
import com.moyu.common.security.exception.ServiceException;
import com.moyu.common.security.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author 赵彦博
 * @Date 2025/3/30 12:32
 */
@Service
public class ReportServiceImpl implements IReportService {

    @Autowired
    private ReportMapper reportMapper;

    @Autowired
    private PostMapper postMapper;

    @Autowired
    private TokenService tokenService;

    @Override
    public List<ReportVO> getReportList(ReportQueryDTO reportQueryDTO) {
        PageHelper.startPage(reportQueryDTO.getPageNum(), reportQueryDTO.getPageSize());
        return reportMapper.getReportList(reportQueryDTO);
    }

    @Override
    public int handleReport(String reportId) {
        String postId = reportMapper.getPostIdByReportId(reportId);
        if (postId == null) {
            throw new ServiceException(ResultCode.FAILED_FIND_POST);
        }

        // 2. 更新帖子状态为待审核(0)
        int postUpdate = postMapper.updatePostState(postId, 0); // 0表示待审核
        if (postUpdate <= 0) {
            throw new ServiceException(ResultCode.FAILED_UPDATE_POST);
        }
        return reportMapper.handleReport(reportId, 1);
    }

    @Override
    public int cancelReport(String reportId) {
        String postId = reportMapper.getPostIdByReportId(reportId);
        if (postId == null) {
            throw new ServiceException(ResultCode.FAILED_FIND_POST);
        }

        // 2. 更新帖子状态为待审核(0)
        int postUpdate = postMapper.updatePostState(postId, 1); // 0表示待审核
        if (postUpdate <= 0) {
            throw new ServiceException(ResultCode.FAILED_UPDATE_POST);
        }
        return reportMapper.handleReport(reportId, 1);
    }

    @Override
    public int addReport(ReportAddDTO reportAddDTO) {
        Report reportVO = new Report();
        Long userId = tokenService.getLoginUserId();
        reportVO.setReportContent(reportAddDTO.getReportContent());
        reportVO.setReporterName(reportAddDTO.getPostTitle());
        reportVO.setPostTitle(reportAddDTO.getPostTitle());
        reportVO.setPostId(Long.valueOf(reportAddDTO.getPostId()));
        reportVO.setReportType(reportAddDTO.getReportType());
        reportVO.setReportState(String.valueOf(0));
        reportVO.setReporterId(userId);
        return reportMapper.insert(reportVO);
    }
}
